*set trace on
clear


****************************************************EXPOSURES
*Enter the path to the original dataset file
import delimited "~\Exposure Dataset V1_0.csv"

	*drop pub_date ex_event ex_type incident target_rep spying_rep sec_rep num_acc spy_nat related
	drop proquest_article_id reminder pub_date related pub_date a_spy_state b_tar_state
	*gen edate = mdy(stmon, stday, styear)
	
	ds, has(type numeric) 
	foreach var in `r(varlist)' { 
    replace `var' = 0 if `var' == .
	}

*Enter the path where to save this file for continued use
save "~\exposures_descriptive_FPA.dta", replace
clear
*Enter the same path as above, this is just to preserve progress
use "~\exposures_descriptive_FPA.dta", replace

gen work1 = styear
recode work1 (1946/1949 = 1 "1946-49") (1950/1959 = 2 "1950-59") (1960/1969 = 3 "1960-69") (1970/1979 = 4 "1970-79") (1980/1989 = 5 "1980-89") (1990/1999 = 6 "1990-99") (2000/2010 = 7 "2000-10"), gen(decade)
drop work1


********************************************************************************
********************************************************************************
gen work1 = styear
recode work1 (1946/1962 = 1 "Early Years (1946-62)") (1963/1975 = 2 "Middle Years (1963-1975)") (1976/1991 = 3 "Late Years (1976-1991)") (1992/2000 = 4 "PostCW HM (1992-2000)") (2001/2010 = 5 "PostCW PostHM (2001-2010)"), gen(cold_war)
drop work1 

gen work1 = styear
recode work1 (1946/1962 = 1 "1946-62") (1962/2010 = 2 "1963-2010"), gen(early_to_late)
drop work1

gen identity = .
gen identity2 = .
gen identity3 = .
gen identity4 = 666666
gen identity5 = .
replace identity = 88888 if (dyad - 200 < 4000)
*replace identity = . if (dyad - 200 > 2800)
replace identity2 = 99999 if (dyad - 200 > 4100)
replace identity3 = 77777 if identity == 88888 | identity2 == 99999
*replace identity2 = . if (dyad - 200 < 2800)
replace identity = 2315 if dyad == 2315
replace identity = 2365 if dyad == 2365
replace identity = 2290 if dyad == 2290
replace identity = 2265 if dyad == 2265
replace identity = 2710 if dyad == 2710
replace identity = 240 if dyad == 240
replace identity = 2310 if dyad == 2310
replace identity = 2355 if dyad == 2355
replace identity = 2360 if dyad == 2360
replace identity = 2630 if dyad == 2630
replace identity = 2731 if dyad == 2731
replace identity = 2339 if dyad == 2339
replace identity = 2645 if dyad == 2645
*replace identity = 2345 if dyad == 2345
replace identity2 = 365002 if dyad == 365002
replace identity2 = 265002 if dyad == 265002
replace identity2 = 40002 if dyad == 40002
replace identity2 = 710002 if dyad == 710002
replace identity2 = 290002 if dyad == 290002
replace identity2 = 731002 if dyad == 731002

replace identity3 = 2315 if dyad == 2315 | dyad == 315002
replace identity3 = 2365 if dyad == 2365 | dyad == 365002 
replace identity3 = 2290 if dyad == 2290 | dyad == 290002
replace identity3 = 2265 if dyad == 2265 | dyad == 265002
replace identity3 = 2710 if dyad == 2710 | dyad == 710002
replace identity3 = 240 if dyad == 240 | dyad == 40002
replace identity3 = 2310 if dyad == 2310 | dyad == 310002
replace identity3 = 2355 if dyad == 2355 | dyad == 355002
replace identity3 = 2360 if dyad == 2360 | dyad == 360002
replace identity3 = 2630 if dyad == 2630 | dyad == 630002
replace identity3 = 2731 if dyad == 2731 | dyad == 731002
replace identity3 = 2339 if dyad == 2339 | dyad == 339002
replace identity3 = 2645 if dyad == 2645 | dyad == 645002
*replace identity3 = 2345 if dyad == 2345 | dyad == 345002

replace identity4 = 2365 if dyad == 2365
replace identity4 = 2710 if dyad == 2710
replace identity4 = 365710 if dyad == 365002 | dyad == 710002

replace identity5 = 2365 if dyad == 2365
replace identity5 = 2710 if dyad == 2710
replace identity5 = 365710 if dyad > 3002

gen num_decade = .
replace num_decade = 1950 if decade == 1
replace num_decade = 1960 if decade == 2
replace num_decade = 1970 if decade == 3
replace num_decade = 1980 if decade == 4
replace num_decade = 1990 if decade == 5
replace num_decade = 2000 if decade == 6
replace num_decade = 2010 if decade == 7

bysort num_decade: egen sum_exposure = total(exposure)

bysort num_decade: egen sum_exposure_cze2 = total(exposure) if dyad == 2315 | dyad == 315002
bysort num_decade: egen sum_exposure_rus2 = total(exposure) if dyad == 2365 | dyad == 365002
bysort num_decade: egen sum_exposure_pol2 = total(exposure) if dyad == 2290 | dyad == 290002
bysort num_decade: egen sum_exposure_gdr2 = total(exposure) if dyad == 2265 | dyad == 265002
bysort num_decade: egen sum_exposure_chn2 = total(exposure) if dyad == 2710 | dyad == 710002

gen all_spying = (us_spying + us_target)
bysort styear: egen sum_all_spying = total(all_spying)
bysort styear: egen sum_us_spying = total(us_spying)
bysort styear: egen sum_us_target = total(us_target)

gen work1 = identity
recode work1 (2315 = 1 "Czechoslovakia") (2365 = 2 "Russia") (2290 = 3 "Poland") (2265 = 4 "East Germany") (2710 = 5 "China") (240 = 6 "Cuba") (2310 = 7 "Hungary") (2355 = 8 "Bulgaria") (2360 = 9 "Romania") (2630 = 10 "Iran") (2731 = 11 "North Korea") (2339 = 12 "Albania") (2645 = 13 "Iraq") (88888 = 14 "All Other States"), gen(usa_accused)
drop work1 

gen work1 = identity2
recode work1 (365002 = 1 "Russia") (265002 = 2 "East Germany") (40002 = 3 "Phillipines") (710002 = 4 "China") (29002 = 5 "Poland") (731002 = 6 "North Korea") (99999 = 7 "All Other States"), gen(usa_targeted)
drop work1 

gen work1 = identity3
recode work1 (2365 = 1 "Russia") (2315 = 2 "Czechoslovakia") (2265 = 3 "East Germany") (2290 = 4 "Poland") (2710 = 5 "China") (240 = 6 "Cuba") (2731 = 7 "North Korea") (2355 = 8 "Bulgaria") (2310 = 9 "Hungary") (2360 = 10 "Romania") (2630 = 11 "Iran") (2645 = 12 "Iraq") (2339 = 13 "Albania") (77777 = 14 "All Other States"), gen(undirected)
drop work1 

gen work1 = identity4
recode work1 (2365 = 1 "Russia") (2710 = 2 "China") (365710 = 3 "US") (666666 = 4 "All Other States"), gen(rus_chn_us_all)
drop work1 

gen work1 = identity5
recode work1 (2365 = 1 "Russia") (2710 = 2 "China") (365710 = 3 "US"), gen(rus_chn_us)
drop work1 

gen new_identity = 1 if dyad == 2365
replace new_identity = 2 if dyad == 2315
replace new_identity = 3 if dyad == 2265
replace new_identity = 4 if dyad == 2290
replace new_identity = 5 if dyad == 2710
replace new_identity = 6 if dyad == 240
replace new_identity = 7 if dyad == 2731
replace new_identity = 8 if dyad == 2355
replace new_identity = 9 if dyad == 2310
replace new_identity = 10 if dyad == 2360
replace new_identity = 11 if dyad == 2630
replace new_identity = 12 if dyad == 2645
replace new_identity = 13 if dyad == 2339
*replace new_identity = 14 if dyad == 2345
replace new_identity = 14 if identity3 == 77777

********************************************************************************
********************************************************************************
*This begins Profession TYPE 1
********************************************************************************
********************************************************************************
*Recoding various jobs accused spies held when accused so I can group them in descriptive statistics as "Profession"
gen job = .
replace job = 10 if aid_worker == 1 
replace job = 20 if tourist_academic == 1
replace job = 30 if business_engineer == 1
replace job = 40 if journalist == 1 
replace job = 50 if clergy == 1  
replace job = 60 if diplomat == 1
replace job = 70 if intelligence  == 1
replace job = 80 if military == 1
replace job = 90 if other_govt == 1

gen work1 = job
recode work1 (10 = 1 "Aid Worker") (20 = 2 "Tourist/Academic") (30 = 3 "Business/Engineer") (40 = 4 "Journalist") (50 = 5 "Religious") (60 = 6 "Diplomat") (70 = 7 "Police/Intelligence") (80 = 8 "Current/Former Military") (90 = 9 "Other Government Employee"), gen(profession)
drop work1 


********************************************************************************
********************************************************************************
*This begins sentencing TYPE 2
********************************************************************************
********************************************************************************

*Recoding minimum sentences spies received after alleged exposure
gen min_yrs = .
replace min_yrs = 60 if released > 0
replace min_yrs = 70 if expelled > 0
replace min_yrs = 90 if commuted > 0
replace min_yrs = 10 if min_years < 6 & min_years > 0.24
replace min_yrs = 20 if min_years < 11 & min_years > 5
replace min_yrs = 30 if min_years < 16 & min_years > 10
replace min_yrs = 40 if min_years < 20 & min_years > 15
replace min_yrs = 50 if min_years > 20
*replace min_yrs = 100 if min_years < 0.25

gen work1 = min_yrs
recode work1 (60 = 1 "Released or Acquitted") (70 = 2 "Expelled") (90 = 3 "Commuted or Time Served") (10 = 4 "0.25 to 5") (20 = 5 "6 to 10") (30 = 6 "11 to 15") (40 = 7 "16 to 20") (50 = 8 "> 20"), gen(min_sentence)
drop work1 


********************************************************************************
********************************************************************************
*Recoding maximum sentences spies received after alleged exposure
gen max_yrs = .
replace max_yrs = 10 if max_years < 5 & min_years > 0.24
replace max_yrs = 20 if max_years < 11 & min_years > 5
replace max_yrs = 30 if max_years < 16 & min_years > 10
replace max_yrs = 40 if max_years < 20 & min_years > 15
replace max_yrs = 50 if max_years > 20
replace max_yrs = 60 if life_sentence == 1
replace max_yrs = 61 if life_sentence == 2
replace max_yrs = 62 if life_sentence == 3
replace max_yrs = 63 if life_sentence > 3
replace max_yrs = 70 if death_sentence == 1
replace max_yrs = 71 if death_sentence == 2
replace max_yrs = 72 if death_sentence == 3
replace max_yrs = 73 if death_sentence == 4
replace max_yrs = 74 if death_sentence == 5
replace max_yrs = 75 if death_sentence == 6
replace max_yrs = 76 if death_sentence == 7
replace max_yrs = 77 if death_sentence == 8
replace max_yrs = 78 if death_sentence == 9
replace max_yrs = 79 if death_sentence == 10
replace max_yrs = 80 if death_sentence > 10
*replace max_yrs = 100 if max_years < 0.25

gen work1 = max_yrs
recode work1 (10 = 1 "0.25 to 5") (20 = 2 "6 to 10") (30 = 3 "11 to 15") (40 = 4 "16 to 20") (50 = 5 "> 20") (60/63 = 6 "Life") (70/80 = 7 "Death"), gen(max_sentence)
drop work1 

gen maxmin = .
replace maxmin = 10 if released > 0
replace maxmin = 11 if commuted > 0
replace maxmin = 20 if expelled > 0
replace maxmin = 30 if life_sentence > 0
replace maxmin = 40 if death_sentence > 0

gen work1 = maxmin
recode work1 (10/11 = 1 "Released") (20 = 2 "Expelled") (30 = 3 "Life Sentence") (40 = 4 "Death Sentence"), gen(min_to_max)
drop work1

gen lifedeath = .
replace lifedeath = 10 if life_sentence > 0
replace lifedeath = 20 if death_sentence > 0

gen work1 = lifedeath
recode work1 (10 = 1 "Life Sentence") (20 = 2 "Death Sentence"), gen(life_to_death)
drop work1

********************************************************************************
********************************************************************************
*This begins other miscelaneous information TYPE 3
********************************************************************************
*Motive, Defection, Trade, Fines, Number Accused
********************************************************************************
gen motivation = .
replace motivation = 10 if motive == 1
replace motivation = 20 if motive == 2
replace motivation = 30 if motive == 3
replace motivation = 40 if motive == 4
replace motivation = 50 if motive == 5
*replace motivation = 60 if motive < 1
*replace motivation = 60 if motive > 5

gen work1 = motivation
recode work1 (10 = 1 "State Agent") (20 = 2 "Money") (30 = 3 "Ideology") (40 = 4 "Coercion/Compromise") (50 = 5 "Ego/Excitement"), gen(reason)
drop work1

********************************************************************************
********************************************************************************
*This Section begins preparation for graphing and labeling
********************************************************************************
********************************************************************************

*us_spying is when US was caught spying
*us_target is when US caught others spying against it
label variable released "Released"
label variable profession "Profession"
label variable min_sentence "Minimum Sentence in Years"
label variable max_sentence "Maximum Sentence in Years"
label variable decade "Decade"
label variable cold_war "Cold War"
label variable exposure "Exposure"
label variable n_dyad "Nuclear Dyad"
label variable us_spying "State Exposed"
label variable us_target "US is Target"
label variable traded "Alleged Spies Traded"
label variable defection "Defection"
label variable expelled "Expelled"
label variable released "Released"
label variable fined "Fined"
label variable payment "Amount of Fine"
label variable reason "Motivation of Accused"
label variable traded "Traded"
label variable usa_accused "US Exposed"
label variable usa_targeted "Foreign State Exposed"
label variable undirected "States"

label define yesno 0 "No" 1 "Yes", replace

label define n_dyad 0 "No" 1 "Yes", replace
label values n_dyad n_dyad

label define exposure 0 "No" 1 "Yes", replace
label values exposure exposure

label define us_target 0 "United States" 1 "Foreign State", replace
label values us_target us_target
label define us_spying 0 "Foreign State" 1 "United States", replace
label values us_spying us_spying

label variable all_spying "All Exposures"
label define all_spying 0 "No" 1 "Yes", replace 
label values all_spying all_spying

label variable us_spying "US is Offender"
label define us_spying 0 "No" 1 "Yes", replace 
label values us_spying us_spying

label variable us_target "US is Target"
label define us_target 0 "No" 1 "Yes", replace 
label values us_target us_target

gen tot_spying = all_spying if all_spying == 1
label variable tot_spying "All Exposures"
label define tot_spying 0 "No" 1 "Yes", replace 
label values tot_spying tot_spying

gen us_is_spying = us_spying if us_spying == 1
label variable us_is_spying "US is Offender"
label define us_is_spying 0 "No" 1 "Yes", replace 
label values us_is_spying us_is_spying

gen us_is_target = us_target if us_target == 1
label variable us_is_target "US is Target"
label define us_is_target 0 "No" 1 "Yes", replace 
label values us_is_target us_is_target


*Enter the same path here as you did above
save "~\exposures_descriptive_FPA.dta", replace
clear
